package com.game.spacetrader;

import java.io.Serializable;

import com.game.spacetrader.map.Coordinates;

import android.util.Log;



/**
 * Class represents the controller for the Game
 *
 */
public class Game implements Serializable {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private Player player;
	private String difficulty;
	private static volatile Game instance = null;
	private Planet curPlanet, travelTo;
	
	/**
	 * constructor
	 * @param p Player object
	 * @param difficulty difficulty of game
	 */
	public Game(Player p, String difficulty){
		setPlayer(p);
		setDifficulty(difficulty);
		instance = this;
	}
	
	public void setGame(Game game) {
		instance = game;
	}
	
	/**
	 * Empty constructor. Should not be called
	 */
	public Game(){
		this(new Player(),"");
		Log.e("Game","No game existed, blank one created");
	}
	
	/**
	 * creates this as a singleton
	 * @return instance of Game
	 */
	public static Game getInstance(){
		if (instance == null) {
			synchronized (Game.class){
				if (instance == null) {
					instance = new Game();
				}
			}
		}
		return instance;
	}
	
	public void setPlayerLocation(Coordinates c){
		player.setLocation(c);
	}
	
	public Coordinates getPlayerLocation(){
		return player.getLocation();
	}
	public Player getPlayer() {
		return player;
	}
	
	public void setPlayer(Player player) {
		this.player = player;
	}

	public String getDifficulty() {
		return difficulty;
	}

	public void setDifficulty(String difficulty) {
		this.difficulty = difficulty;
	}
	
	public Planet getCurrentPlanet() {
		return curPlanet;
	}

	public void setCurrentPlanet(Planet p) {
		curPlanet = p;
	}
	public void start(){//TODO
		
	}

	public Planet getTravelTo() {
		return travelTo;
	}

	public void setTravelTo(Planet travelTo) {
		this.travelTo = travelTo;
	}
	
	/**
	 * Travels to the inputted planet
	 * @param p new planet
	 * @param d distance
	 * @return if enough gas
	 */
	public boolean travel(Planet p, double d){
		if(player.getGas()-d >=0){
			Log.d("Game travel", "enough gas");
			curPlanet = p;
			player.setLocation(p.getCoord());
			player.setGas((int) (player.getGas()-d));
			setCurrentPlanet(p);
			return true;
		}
		Log.d("Game travel", "not enough gas");
		return false;
	}
}
